package medium;

/**
 * @author lzy
 * @version v1.0 Create DateTime: 2019/2/26 11:14
 */
class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    @Override
    public String toString() {
        ListNode node = this;
        StringBuilder builder = new StringBuilder();
        builder.append(this.val).append("-");
        while ((node = node.next) != null) {
            builder.append(node.val).append("-");
        }
        return builder.substring(0, builder.length() - 1);
    }
}

public class AddTwoNumbers {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(8);
        //        l1.next.next = new ListNode(3);
        //        l1.next.next.next = new ListNode(9);
        //        l1.next.next.next.next = new ListNode(3);
        //        l1.next.next.next.next.next = new ListNode(2);

        ListNode l2 = new ListNode(0);
        //        l2.next = new ListNode(6);
        //        l2.next.next = new ListNode(4);

        Solution2 solution = new AddTwoNumbers().new Solution2();
        ListNode node = solution.addTwoNumbers(l1, l2);

        System.out.println("l1:" + l1);
        System.out.println("l2:" + l2);
        System.out.println(node);
    }

    private class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode x = l1;
            ListNode y = l2;

            int temp = x.val + y.val;
            ListNode present = new ListNode(temp % 10);
            int headout = temp >= 10 ? 1 : 0;
            ListNode last = present;
            ListNode head = present;
            x = x.next;
            y = y.next;

            while ((x != null) && (y != null)) {
                temp = x.val + y.val + headout;
                present = new ListNode(temp % 10);
                headout = temp >= 10 ? 1 : 0;
                last.next = present;
                last = present;
                x = x.next;
                y = y.next;
            }

            if (x == null && y == null) {
                if (headout == 1) {
                    setLastNode(last);
                }
                return head;
            }

            if (x != null) {
                if (headout == 0) {
                    last.next = x;
                    return head;
                }

                do {
                    temp = x.val + headout;
                    present = new ListNode(temp % 10);
                    headout = temp >= 10 ? 1 : 0;
                    last.next = present;
                    last = present;
                } while ((x = x.next) != null);

                if (headout == 1) {
                    setLastNode(last);
                }
                return head;
            }

            if (y != null) {
                if (headout == 0) {
                    last.next = y;
                    return head;
                }

                do {
                    temp = y.val + headout;
                    present = new ListNode(temp % 10);
                    headout = temp >= 10 ? 1 : 0;
                    last.next = present;
                    last = present;
                } while ((y = y.next) != null);

                if (headout == 1) {
                    setLastNode(last);
                }
            }
            return head;
        }

        private void setLastNode(ListNode node) {
            ListNode present = new ListNode(1);
            node.next = present;
        }
    }

    private class Solution2 {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode x = l1, y = l2;
            int temp = x.val + y.val;
            int headout = temp >= 10 ? 1 : 0;
            ListNode present = new ListNode(temp % 10);
            ListNode last = present;
            ListNode head = present;
            while (x.next != null || y.next != null) {
                if (x.next == null && headout == 0) {
                    last.next = y.next;
                    return head;
                }

                if (y.next == null && headout == 0) {
                    last.next = x.next;
                    return head;
                }

                x = x.next == null ? new ListNode(0) : x.next;
                y = y.next == null ? new ListNode(0) : y.next;

                temp = x.val + y.val + headout;
                present = new ListNode(temp % 10);
                headout = temp >= 10 ? 1 : 0;
                last.next = present;
                last = present;
            }

            if (headout == 1) {
                last.next = new ListNode(1);
            }

            return head;
        }
    }
}
